package com.phone_verification_code;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName: YunZhiXunVerificationCodeForPhoneNumber
 * @Author: ZhangYue26
 * @Description: 云之讯短信验证码,目前账号未认证，所以不能直接使用
 * @Date: 2020-10-09
 */
public class YunZhiXunVerificationCodeForPhoneNumber {
    public static void main(String[] args) throws Exception {
        URL url = new URL("https://open.ucpaas.com/ol/sms/sendsms");
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setUseCaches(false);
        httpURLConnection.setRequestProperty("Content-Type","application/json;charset=utf-8");
        OutputStream outputStream = httpURLConnection.getOutputStream();
        Map paramMap = new HashMap();
        paramMap.put("sid", "21daa19b34f09ba781670137f0b0a151");
        paramMap.put("token", "dfbab02a2d3d63c0a809441af45362ae");
        paramMap.put("appid", "069b80c69ff24e3f8db848a3c74fc3df");
        paramMap.put("templateid", "569642");
        paramMap.put("param", "456789");
        paramMap.put("mobile", "13131503075");
        paramMap.put("uid", UUID.randomUUID().toString().replaceAll("-", ""));
        ObjectMapper objectMapper = new ObjectMapper();
        String paramsJson = objectMapper.writeValueAsString(paramMap);
        outputStream.write(paramsJson.getBytes());
        outputStream.flush();
        outputStream.close();
        byte[] bytes = new byte[8 * 1024];
        httpURLConnection.connect();
        InputStream inputStream = httpURLConnection.getInputStream();
        while (inputStream.read(bytes) != -1) {
            System.out.println(new String(bytes, "UTF-8"));
            bytes = new byte[8 * 1024];
        }
        inputStream.close();

    }
}
